<?php

namespace ctrl;

use model\article_cate;
use libs\view;

class contentcate extends base
{
	private static $themeDir = P_ROOT . "/app/index/view/default/contents/"; //内容列表模板路径
	private static $sonThemeDir = P_ROOT . "/app/index/view/default/content/"; //内容列表模板路径
	private static $M;
	public static function Init()
	{
		parent::init();
		self::$M = new article_cate;
	}

	public static function index()
	{
		if (isset($_GET['list_json'])) {
			$list = self::$M->SelectData(1);
			$jsonlist = array();
			$jsonlist['code'] = 0;
			$jsonlist['msg'] = '';
			if (!isset($_GET['mod'])) //根据传递值判断生成输出数据模式，用于LAYUI tree调用
				$jsonlist['data'] = GenerateTree($list);
			else
				$jsonlist['data'] = ContentCateTree($list); //栏目管理调用
			exit(json_encode($jsonlist));
		}
		view::display();
	}

	public static function add()
	{
		if (isset($_GET['view'])) {
			$list = self::$M->SelectTreeData();
			view::assign('list', $list);
			view::assign('templist', GetFileFolderList(static::$themeDir, 0, "*.html"));
			view::assign('sontemplist', GetFileFolderList(static::$sonThemeDir, 0, "*.html"));
			view::assign('info', null);
			view::assign('title', "添加栏目");
			view::display("post");
		} else {
			$result = self::$M->insertData();
			Response(array('status' => $result['status'], 'info' => $result['msg']));
		}
	}

	public static function edit()
	{
		if (isset($_GET['view'])) {
			$info = self::$M->FindData();
			$list = self::$M->SelectTreeData();
			view::assign('list', $list);
			view::assign('info', $info);
			view::assign('templist', GetFileFolderList(static::$themeDir, 0, "*.html"));
			view::assign('sontemplist', GetFileFolderList(static::$sonThemeDir, 0, "*.html"));
			view::assign('title', "编辑栏目");
			view::display("post");
		} elseif (isset($_GET['sortedit'])) {
			$result = self::$M->updateSort();
			Response(array('status' => $result['status'], 'info' => $result['msg']));
		} else {
			$result = self::$M->updateData();
			Response(array('status' => $result['status'], 'info' => $result['msg']));
		}
	}


	public static function del()
	{
		if (self::$M->deleteOneData())
			Response(['status' => 1, 'info' => '删除成功']);
		else
			Response(['status' => 0, 'info' => '该栏目下有内容，拒绝删除！']);
	}
}
